import asyncio
import websockets
 
async def hello(websocket, path):
    # 服务端收到客户端的信息
    name = await websocket.recv()
    print(f"客户端: {name}")
    # 服务端发送给客户端的信息

    names = ["Alice", "Bob", "Charlie"] 
    for name in names:
     greeting = f"Hello {name}!"
     await asyncio.sleep(1)  # 等待500毫秒
     await websocket.send(greeting)
     print(f"服务端: {greeting}")
 
start_server = websockets.serve(hello, "localhost", 8765)
 
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()